// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.ai.states;

import java.util.HashMap;
import zombie.ai.State;
import zombie.characters.IsoGameCharacter;
import zombie.characters.IsoPlayer;
import zombie.characters.skills.PerkFactory;
import zombie.iso.IsoCell;
import zombie.iso.IsoDirections;
import zombie.iso.IsoGridSquare;
import zombie.iso.IsoWorld;
import zombie.iso.SpriteDetails.IsoFlagType;
import zombie.iso.objects.IsoWindow;

public final class ClimbDownSheetRopeState extends State {
    public static final float CLIMB_DOWN_SPEED = 0.16F;
    private static final float CLIMB_DOWN_SLOWDOWN = 0.5F;
    private static final ClimbDownSheetRopeState _instance = new ClimbDownSheetRopeState();

    public static ClimbDownSheetRopeState instance() {
        return _instance;
    }

    @Override
    public void enter(IsoGameCharacter owner) {
        owner.setIgnoreMovement(true);
        owner.setHideWeaponModel(true);
        owner.setbClimbing(true);
        owner.setVariable("ClimbRope", true);
    }

    @Override
    public void execute(IsoGameCharacter owner) {
        HashMap hashMap = owner.getStateMachineParams(this);
        float float0 = 0.0F;
        float float1 = 0.0F;
        if (owner.getCurrentSquare().getProperties().Is(IsoFlagType.climbSheetTopN) || owner.getCurrentSquare().getProperties().Is(IsoFlagType.climbSheetN)) {
            owner.setDir(IsoDirections.N);
            float0 = 0.54F;
            float1 = 0.39F;
        }

        if (owner.getCurrentSquare().getProperties().Is(IsoFlagType.climbSheetTopS) || owner.getCurrentSquare().getProperties().Is(IsoFlagType.climbSheetS)) {
            owner.setDir(IsoDirections.S);
            float0 = 0.118F;
            float1 = 0.5756F;
        }

        if (owner.getCurrentSquare().getProperties().Is(IsoFlagType.climbSheetTopW) || owner.getCurrentSquare().getProperties().Is(IsoFlagType.climbSheetW)) {
            owner.setDir(IsoDirections.W);
            float0 = 0.4F;
            float1 = 0.7F;
        }

        if (owner.getCurrentSquare().getProperties().Is(IsoFlagType.climbSheetTopE) || owner.getCurrentSquare().getProperties().Is(IsoFlagType.climbSheetE)) {
            owner.setDir(IsoDirections.E);
            float0 = 0.5417F;
            float1 = 0.3144F;
        }

        float float2 = owner.x - (float)((int)owner.x);
        float float3 = owner.y - (float)((int)owner.y);
        if (float2 != float0) {
            float float4 = (float0 - float2) / 4.0F;
            float2 += float4;
            owner.x = (float)((int)owner.x) + float2;
        }

        if (float3 != float1) {
            float float5 = (float1 - float3) / 4.0F;
            float3 += float5;
            owner.y = (float)((int)owner.y) + float3;
        }

        owner.nx = owner.x;
        owner.ny = owner.y;
        float float6 = this.getClimbDownSheetRopeSpeed(owner);
        owner.getSpriteDef().AnimFrameIncrease = float6;
        float float7 = owner.z - float6 / 10.0F * zombie.GameTime.instance.getMultiplier();
        float7 = Math.max(float7, 0.0F);

        for (int _int = (int)owner.z; _int >= (int)float7; _int--) {
            IsoCell isoCell = IsoWorld.instance.getCell();
            IsoGridSquare isoGridSquare = isoCell.getGridSquare((double)owner.getX(), (double)owner.getY(), (double)_int);
            if ((isoGridSquare.Is(IsoFlagType.solidtrans) || isoGridSquare.TreatAsSolidFloor() || _int == 0) && float7 <= (float)_int) {
                owner.z = (float)_int;
                hashMap.clear();
                owner.clearVariable("ClimbRope");
                owner.setCollidable(true);
                owner.setbClimbing(false);
                return;
            }
        }

        owner.z = float7;
        if (!IsoWindow.isSheetRopeHere(owner.getCurrentSquare())) {
            owner.setCollidable(true);
            owner.setbClimbing(false);
            owner.setbFalling(true);
            owner.clearVariable("ClimbRope");
        }

        if (owner instanceof IsoPlayer && ((IsoPlayer)owner).isLocalPlayer()) {
            ((IsoPlayer)owner).dirtyRecalcGridStackTime = 2.0F;
        }
    }

    @Override
    public void exit(IsoGameCharacter owner) {
        owner.setIgnoreMovement(false);
        owner.setHideWeaponModel(false);
        owner.clearVariable("ClimbRope");
        owner.setbClimbing(false);
    }

    public float getClimbDownSheetRopeSpeed(IsoGameCharacter owner) {
        float _float = 0.16F;
        switch (owner.getPerkLevel(PerkFactory.Perks.Strength)) {
            case 0:
                _float -= 0.12F;
                break;
            case 1:
            case 2:
            case 3:
                _float -= 0.09F;
            case 4:
            case 5:
            default:
                break;
            case 6:
            case 7:
                _float += 0.05F;
                break;
            case 8:
            case 9:
                _float += 0.09F;
                break;
            case 10:
                _float += 0.12F;
        }

        return _float * 0.5F;
    }
}
